package org.hzz.coupon.infrastructure.dao.mapper.code;

import jakarta.annotation.Generated;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.hzz.coupon.infrastructure.dao.model.code.ExchangeCode;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.*;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static org.hzz.coupon.infrastructure.dao.mapper.code.ExchangeCodeDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface ExchangeCodeDynamicMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<ExchangeCode>, CommonUpdateMapper {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn.columnList(id, code, status, userId, type, exchangeTargetId, createTime, expiredTime, updateTime);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="ExchangeCodeResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
        @Result(column="type", property="type", jdbcType=JdbcType.TINYINT),
        @Result(column="exchange_target_id", property="exchangeTargetId", jdbcType=JdbcType.BIGINT),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="expired_time", property="expiredTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<ExchangeCode> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("ExchangeCodeResult")
    Optional<ExchangeCode> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Integer id_) {
        return delete(c -> 
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(ExchangeCode row) {
        return MyBatis3Utils.insert(this::insert, row, exchangeCode, c ->
            c.map(id).toProperty("id")
            .map(code).toProperty("code")
            .map(status).toProperty("status")
            .map(userId).toProperty("userId")
            .map(type).toProperty("type")
            .map(exchangeTargetId).toProperty("exchangeTargetId")
            .map(createTime).toProperty("createTime")
            .map(expiredTime).toProperty("expiredTime")
            .map(updateTime).toProperty("updateTime")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertMultiple(Collection<ExchangeCode> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, exchangeCode, c ->
            c.map(id).toProperty("id")
            .map(code).toProperty("code")
            .map(status).toProperty("status")
            .map(userId).toProperty("userId")
            .map(type).toProperty("type")
            .map(exchangeTargetId).toProperty("exchangeTargetId")
            .map(createTime).toProperty("createTime")
            .map(expiredTime).toProperty("expiredTime")
            .map(updateTime).toProperty("updateTime")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(ExchangeCode row) {
        return MyBatis3Utils.insert(this::insert, row, exchangeCode, c ->
            c.map(id).toPropertyWhenPresent("id", row::getId)
            .map(code).toPropertyWhenPresent("code", row::getCode)
            .map(status).toPropertyWhenPresent("status", row::getStatus)
            .map(userId).toPropertyWhenPresent("userId", row::getUserId)
            .map(type).toPropertyWhenPresent("type", row::getType)
            .map(exchangeTargetId).toPropertyWhenPresent("exchangeTargetId", row::getExchangeTargetId)
            .map(createTime).toPropertyWhenPresent("createTime", row::getCreateTime)
            .map(expiredTime).toPropertyWhenPresent("expiredTime", row::getExpiredTime)
            .map(updateTime).toPropertyWhenPresent("updateTime", row::getUpdateTime)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<ExchangeCode> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<ExchangeCode> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<ExchangeCode> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<ExchangeCode> selectByPrimaryKey(Integer id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, exchangeCode, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(ExchangeCode row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(row::getId)
                .set(code).equalTo(row::getCode)
                .set(status).equalTo(row::getStatus)
                .set(userId).equalTo(row::getUserId)
                .set(type).equalTo(row::getType)
                .set(exchangeTargetId).equalTo(row::getExchangeTargetId)
                .set(createTime).equalTo(row::getCreateTime)
                .set(expiredTime).equalTo(row::getExpiredTime)
                .set(updateTime).equalTo(row::getUpdateTime);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(ExchangeCode row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(row::getId)
                .set(code).equalToWhenPresent(row::getCode)
                .set(status).equalToWhenPresent(row::getStatus)
                .set(userId).equalToWhenPresent(row::getUserId)
                .set(type).equalToWhenPresent(row::getType)
                .set(exchangeTargetId).equalToWhenPresent(row::getExchangeTargetId)
                .set(createTime).equalToWhenPresent(row::getCreateTime)
                .set(expiredTime).equalToWhenPresent(row::getExpiredTime)
                .set(updateTime).equalToWhenPresent(row::getUpdateTime);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(ExchangeCode row) {
        return update(c ->
            c.set(code).equalTo(row::getCode)
            .set(status).equalTo(row::getStatus)
            .set(userId).equalTo(row::getUserId)
            .set(type).equalTo(row::getType)
            .set(exchangeTargetId).equalTo(row::getExchangeTargetId)
            .set(createTime).equalTo(row::getCreateTime)
            .set(expiredTime).equalTo(row::getExpiredTime)
            .set(updateTime).equalTo(row::getUpdateTime)
            .where(id, isEqualTo(row::getId))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(ExchangeCode row) {
        return update(c ->
            c.set(code).equalToWhenPresent(row::getCode)
            .set(status).equalToWhenPresent(row::getStatus)
            .set(userId).equalToWhenPresent(row::getUserId)
            .set(type).equalToWhenPresent(row::getType)
            .set(exchangeTargetId).equalToWhenPresent(row::getExchangeTargetId)
            .set(createTime).equalToWhenPresent(row::getCreateTime)
            .set(expiredTime).equalToWhenPresent(row::getExpiredTime)
            .set(updateTime).equalToWhenPresent(row::getUpdateTime)
            .where(id, isEqualTo(row::getId))
        );
    }
}